package cn.yunhe.services.impl;

import cn.yunhe.dao.IPaperDao;
import cn.yunhe.dao.IProblemDao;
import cn.yunhe.entity.AddPaPer;
import cn.yunhe.entity.Paper;
import cn.yunhe.entity.Problem;
import cn.yunhe.entity.StudentProblem;
import cn.yunhe.services.IPaperServices;
import cn.yunhe.util.ListToXml;
import cn.yunhe.util.StringToMap;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.*;

/**
 * Created by 16555 on 2018/5/23.
 */
@Service("iPaperServices")
public class PaperServices implements IPaperServices {

    @Resource
    private IProblemDao problemDao;

    @Resource
    private IPaperDao paperDao;


    @Override
    public int addPaper(AddPaPer addPaPer) {
        try {
            XsTreamServices xsTreamServices=new XsTreamServices();
            List<StudentProblem> problemsd = new ArrayList<StudentProblem>();
            //类型题目以及数量

            //知识点集合
            List<Integer> zhishidian = addPaPer.getPoints();
            //取出的问题转换成xml
            List<List> li = new ArrayList<List>();

            //查询某知识点下的所有题目数量
            for (int t = 0; t < zhishidian.size(); t++) {
                List<Problem> problems = new ArrayList<Problem>();
                List<List> lii = new ArrayList<List>();
                //查询list集合中所包含的知识点号所对应的试题集合
                //查询符合已选择知识点下的所有已选择的题型的题目集合
                for (int tc = 0; tc < addPaPer.getId().size(); tc++) {
                    Map map = new HashMap<>();
                    map.put("type_id", addPaPer.getId().get(tc));
                    map.put("point_id", zhishidian.get(t));
                    problems = problemDao.getPointAndTypeList(map);
                    if (problems.size() != 0) {
                        //转换problem到map集合中
                        for (int i = 0; i < problems.size(); i++) {
                            //选项
                            problems.get(i).setPro_mapname(StringToMap.stringToMap( problems.get(i).getPro_choice()));
                            //图片
                            problems.get(i).setPro_img(StringToMap.imgToMap(problems.get(i).getPro_choice()));
                        }
                    }
                    lii.add(problems);
                }

                li.add(lii);
                lii = new ArrayList<List>();
            }
            double o[] = new double[zhishidian.size()];
            //平均每个知识点出现的概况
            for (int i = 0; i < zhishidian.size(); i++) {
                o[i] = (double) (i + 1) / zhishidian.size();
                //每个知识点的value值做list的name，，存储该知识点，该难度下的所有题目。
            }

            //遍历所有的题型
            Problem problem = new Problem();
            for (int j = 0; j < addPaPer.getId().size(); j++) {

                //遍历该题型下应该有的题目数量集合，并存储到集合内
                if (addPaPer.getDao().get(j) != null) {
                    //判断该题型下面的题目数量是否为空

                        for (int i = 0; i < addPaPer.getDao().get(j); i++) {
                            try {
                            int z = 0;
                            //轮盘赌法随机知识点的出现
                            double ron = Math.random();
                            //随机出现的知识点
                            for (z = 0; z < zhishidian.size(); z++) {
                                double rcd = Math.random();
                                if (ron < o[z]) {
                                    if (li.get((int) (o[z] * zhishidian.size() - 1)).size() >= 0) {
                                        //判断结果是否溢出
                                        if (li.get((int) (o[z] * zhishidian.size() - 1)).get(j) != null) {
                                            List<Problem> lis = (List<Problem>) li.get((int) (o[z] * zhishidian.size() - 1)).get(j);
                                            //get该id属于的题目对象
                                            System.out.println(".......................................................................");
                                            int rc = (int) rcd * lis.size();

                                            if (lis.get(rc) != null) {
                                                System.out.println(lis.get(rc));
                                                System.out.println("00000000000000000000000000000000000000000000000000000000000000000000000");
                                                problem = lis.get(rc);
                                                ((List<Problem>) li.get((int) (o[z] * zhishidian.size() - 1)).get(j)).remove(rc);
                                                break;
                                            }


                                        }

                                    }

                                }
                            }


                            StudentProblem studentProblem = new StudentProblem();
                            //考题id
                            studentProblem.setPid(problem.getPro_id());
                            //正确答案
                            studentProblem.setCorrectAnswer(problem.getP_true());
                            //等级
                            studentProblem.setGrade(problem.getGrade());
                            //选项
                            studentProblem.setPchoice(problem.getPro_mapname());
                            //图片
                            studentProblem.setImages((LinkedHashMap<String, String>) problem.getPro_img());
                            //题目名称
                            studentProblem.setPname(problem.getPro_name());
                            //学生答案
                            studentProblem.setStudentAnswer(null);
                            //考题类型
                            studentProblem.setType(problem.getType().getType_name());
                            //解析
                            studentProblem.setResolve(problem.getTrue_res());


                            problemsd.add(studentProblem);

                            //去除该查询出的该集合下的已选择的题目，防止重复


                        }catch(Exception e){
                                continue;
                    }
                    }
                    }

            }
            Map map = new HashMap();
            Date date = new Date(new java.util.Date().getTime());
            map.put("creat_time", date);
            map.put("content", ListToXml.listToXml(problemsd));
            map.put("paper_name", addPaPer.getPaper_name());
            map.put("ptype_id", addPaPer.getPtype_id());
            map.put("teacher_id", 2);
            map.put("struc", addPaPer.getTtype());
            map.put("pass", addPaPer.getPass());
            map.put("full", addPaPer.getFull());
            map.put("dura", addPaPer.getDura());
            map.put("class_id", addPaPer.getClass_id());
            map.put("p_key", addPaPer.getPwd());
            map.put("online_state", 0);
            System.out.println("meiy");
            return paperDao.addPaper(map);

        } catch (Exception e) {
            return 0;
        }


    }

    @Override
    public Page getListPapers(Map map) {
        Page page = new Page();
        //总条数
        page.setTotal(paperDao.getTotal(map));
        //每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        //当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        page.setPageNum(curPageNum);
        //当前翻页
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        //分页
        page = PageHelper.startPage(curPageNum, pageSize);
        //所有信息数据集合
        List<Paper> list = paperDao.getListPapers(map);
        return page;
    }
    @Override
    public List<Paper> getPaperByClassId(int cid) {
        return paperDao.getPaperByClassId(cid);
    }

    @Override
    public int delePaper(int id) {
        return paperDao.delePaper(id);
    }

    @Override
    public Paper showOnePaper(int id) {
        return paperDao.showOnePaper(id);
    }

    @Override
    public int editPaper(Paper paper) {
        return paperDao.editPaper(paper);
    }

    @Override
    public int editOnline(Paper paper) {
        return paperDao.editOnline(paper);
    }

    @Override
    public Paper showOnePaperInfo(Paper paper) {
        return paperDao.showOnePaperInfo(paper);
    }

    @Override
    public int saddPaper(Paper paper) {
        return paperDao.saddPaper(paper);
    }

    @Override
    public int sdEditPaper(Paper paper) {
        return paperDao.sdEditPaper(paper);
    }

}
